package com.example.ben.alltest;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.ben.alltest.R;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ben on 2016/12/18.
 */

public class ManageInfo  extends AppCompatActivity {

    private ViewPager mPager;//选项卡内容
    private List<View> listView;//Tab页面列表
    private TextView t1,t2,t3;//页卡头标

    //初始化头标
    private void InitTextView(){
        t1=(TextView)findViewById(R.id.text1);
        t2=(TextView)findViewById(R.id.text2);
        t3=(TextView)findViewById(R.id.text3);
        t1.setTextColor(Color.BLUE);
        t1.setOnClickListener(new MyOnClickListener(0));
        t2.setOnClickListener(new MyOnClickListener(1));
        t3.setOnClickListener(new MyOnClickListener(2));
    }
    /*
    * 头标的监听
    * */
    public class MyOnClickListener implements View.OnClickListener {
        private int index=0;
        int o=0;
        public MyOnClickListener(int i) {
            index=i;
        }
        @Override
        public void onClick(View view) {
            mPager.setCurrentItem(index);
            t1.setTextColor(Color.BLACK);
            t2.setTextColor(Color.BLACK);
            t3.setTextColor(Color.BLACK);
            if(index==0){
                t1.setTextColor(Color.BLUE);
            }else if(index==1){
                t2.setTextColor(Color.BLUE);
            }else if(index==2){
                t3.setTextColor(Color.BLUE);
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infoshow);
        InitTextView();
        InitViewPager();
    }

    //初始化ViewPage
    private void InitViewPager(){
        mPager=(ViewPager)findViewById(R.id.vPager);
        listView=new ArrayList<View>();
        LayoutInflater mInflater=getLayoutInflater();
        //设置每个页面中ListView的信息
        View Page1=mInflater.inflate(R.layout.seeinfopage,null);
        ListView List1=(ListView)Page1.findViewById(R.id.SeeInfo);
        //-----例如开始
        ArrayAdapter<String> arrayAdapter;
        ArrayList al=new ArrayList<String>();
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        al.add("1");
        al.add("2");
        al.add("3");
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,al);
        List1.setAdapter(arrayAdapter);
        //-----结束
        View Page2=mInflater.inflate(R.layout.changeinfopage,null);

        View Page3=mInflater.inflate(R.layout.deleteinfopage,null);

        //加载页面
        listView.add(Page1);
        listView.add(Page2);
        listView.add(Page3);

        mPager.setAdapter(new MyPagerAdapter(listView));
        mPager.setCurrentItem(0);
        //mPager.setOnPageChangeListener(new MyOnPagerChangeListener());
    }

    private class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;
        public MyPagerAdapter(List<View> listView) {
            this.mListViews=listView;
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public void startUpdate(View container) {
            super.startUpdate(container);
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager)container).addView(mListViews.get(position),0);
            return mListViews.get(position);

        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager)container).removeView(mListViews.get(position));
        }

        @Override
        public void finishUpdate(View container) {
            super.finishUpdate(container);
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==(object);
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
            super.restoreState(state, loader);
        }
    }

}
